package com.example.springboottx.controller;

import com.example.springboottx.bean.Computer;
import com.example.springboottx.service.ComputerService;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

@Controller
public class ComputerController {

    @Autowired
    private ComputerService computerService;

    @GetMapping("/computer")
    @ResponseBody
    public String insertComputer(){
        Computer computer=new Computer();
        computer.setComputerName("华硕");
        computer.setComputerPrice(5000);
        computer.setKeyboard("雷蛇键盘");
        computer.setMouse("雷蛇鼠标");
        int i = computerService.insertComputer(computer);
        return "自增的id: "+computer.getComputerId();
    }

    @GetMapping("/computer2")
    @ResponseBody
    public String insertComputer2(){
        Computer computer=new Computer();
        computer.setComputerName("华硕");
        computer.setComputerPrice(5000);
        computer.setKeyboard("雷蛇键盘");
        computer.setMouse("雷蛇鼠标");
        long l = computerService.insertComputerTestSelectKey(computer);
        return "自增的id: "+computer.getComputerId();
    }

    @GetMapping("/computer/{name}")
    @ResponseBody
    public String insertComputer(@PathVariable(value = "name") String name){
        Computer computer = computerService.selectOneComputer(name);
        System.out.println(computer);
        return computer.toString();
    }

    @GetMapping("/findBy2Param")
    @ResponseBody
    public String findBy2Param(@RequestParam("computerName") String computerName,
                               @RequestParam("computerPrice") Integer computerPrice){
        Computer oneBy2Param = computerService.findOneBy2Param(computerName, computerPrice);
        System.out.println(oneBy2Param);
        return oneBy2Param.toString();
    }

    @GetMapping("/allComputer")
    @ResponseBody
    public String findAllComputer(@RequestParam("keyboard") String keyboard,@RequestParam("mouse") String mouse){
        List<Computer> computers = computerService.selectAllComputer(keyboard, mouse);
        return computers.toString();
    }

    @GetMapping("/findByMap")
    @ResponseBody
    public String findByMap(@RequestParam("computerName") String computerName,
                            @RequestParam("computerPrice") String computerPrice){
        HashMap<String, String> stringStringHashMap = new HashMap<>();
        stringStringHashMap.put("computerName",computerName);
        stringStringHashMap.put("computerPrice",computerPrice);
        Computer byMap = computerService.findByMap(stringStringHashMap);
        System.out.println("byMap "+byMap);
        return byMap.toString();
    }
}
